<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:fragment="layout(title, content)">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title th:replace="${title}">图书馆管理系统</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <!-- JavaScript -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Moment.js for date formatting -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
    <!-- 自定义样式 -->
    <style>
        .navbar-brand {
            font-weight: bold;
        }

        .footer {
            background-color: #f8f9fa;
            padding: 20px 0;
            margin-top: 30px;
            border-top: 1px solid #e7e7e7;
        }

        .main-content {
            min-height: calc(100vh - 180px);
            padding: 20px 0;
        }

        /* 登录和注册页面特殊样式 */
        body.auth-page .navbar,
        body.auth-page .footer {
            display: none;
        }

        body.auth-page .main-content {
            padding: 0;
            min-height: 100vh;
        }

        body.auth-page .container {
            max-width: 100%;
            padding: 0;
        }
    </style>
</head>

<body th:class="${#request.requestURI == '/login' || #request.requestURI == '/register'} ? 'auth-page' : ''">
    <!-- 导航栏 - 根据登录状态显示或隐藏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light" id="mainNav">
        <div class="container">
            <a class="navbar-brand" href="/">图书馆管理系统</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav mr-auto" id="mainMenu">
                    <li class="nav-item">
                        <a class="nav-link" href="/"><i class="fas fa-home"></i> 首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/book/list"><i class="fas fa-book"></i> 图书管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/category/list"><i class="fas fa-list"></i> 分类管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/user/list"><i class="fas fa-users"></i> 用户管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/borrow/list"><i class="fas fa-clipboard-list"></i> 借阅管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/comment/list"><i class="fas fa-comments"></i> 评论管理</a>
                    </li>
                </ul>
                <ul class="navbar-nav" id="userMenu">
                    <!-- 未登录状态显示 -->
                    <li class="nav-item" id="loginMenuItem">
                        <a class="nav-link" href="/login"><i class="fas fa-sign-in-alt"></i> 登录</a>
                    </li>
                    <li class="nav-item" id="registerMenuItem">
                        <a class="nav-link" href="/register"><i class="fas fa-user-plus"></i> 注册</a>
                    </li>
                    <!-- 已登录状态显示 -->
                    <li class="nav-item dropdown" id="userDropdown" style="display: none;">
                        <a class="nav-link dropdown-toggle" href="#" id="userDropdownLink" role="button"
                            data-toggle="dropdown">
                            <i class="fas fa-user"></i> <span id="currentUsername">用户</span>
                        </a>
                        <div class="dropdown-menu dropdown-menu-right">
                            <a class="dropdown-item" href="/user/profile"><i class="fas fa-id-card"></i> 个人信息</a>
                            <a class="dropdown-item" href="/user/borrow"><i class="fas fa-book-reader"></i> 我的借阅</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="#" onclick="logout()"><i class="fas fa-sign-out-alt"></i>
                                退出登录</a>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <div class="container main-content">
        <div th:replace="${content}">
            <!-- 页面内容将在这里替换 -->
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="footer">
        <div class="container">
            <div class="row">
                <div class="col-md-12 text-center">
                    <p>b24015104 杨赟</p>
                </div>
            </div>
        </div>
    </footer>

    <!-- 自定义脚本 -->
    <script>
        $(document).ready(function () {
            // 根据当前页面URL高亮导航菜单
            const currentPath = window.location.pathname;
            $('.navbar-nav .nav-link').each(function () {
                const href = $(this).attr('href');
                if (currentPath.startsWith(href) && href !== '/') {
                    $(this).parent().addClass('active');
                } else if (currentPath === '/' && href === '/') {
                    $(this).parent().addClass('active');
                }
            });

            // 检查登录状态
            checkLoginStatus();
        });

        // 检查登录状态
        function checkLoginStatus() {
            // 从localStorage获取登录信息
            const username = localStorage.getItem('username');
            const isLoggedIn = localStorage.getItem('isLoggedIn');

            // 如果是登录页面，不显示导航菜单
            if (window.location.pathname === '/login' || window.location.pathname === '/register') {
                $('#mainMenu').hide();
            }

            // 根据登录状态显示不同的菜单
            if (isLoggedIn === 'true' && username) {
                // 已登录状态
                $('#loginMenuItem, #registerMenuItem').hide();
                $('#userDropdown').show();
                $('#currentUsername').text(username);
            } else {
                // 未登录状态
                $('#loginMenuItem, #registerMenuItem').show();
                $('#userDropdown').hide();
            }
        }

        // 退出登录
        function logout() {
            // 清除登录信息
            localStorage.removeItem('username');
            localStorage.removeItem('isLoggedIn');

            // 跳转到登录页
            window.location.href = '/login';
        }
    </script>
</body>

</html>